<?php
	/**
	* Update logs
	* Oct 22, 2010 - trung - remove background_id 
	* Feb 10, 2011 - trungdt - Remove Feature checkbox & tag
	* Feb 11, 2011 - trungdt - Alter SQL
	*                       ALTER TABLE `ag_newsitem` CHANGE `internal_name` `internal_name` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL 
	*                       Implement internal name, to make news works with SEO
	*/
	class newsitem {
		function form($object,$config) {
			$i18n = exponent_lang_loadFile('datatypes/newsitem.php');

			global $user;

			if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
			exponent_forms_initialize();

			$form = new form();
			if (!isset($object->id)) {
				$object->title = '';
				$object->internal_name = '';
				$object->body = '';
				$object->publish = null;
				$object->unpublish = null;
				$object->is_featured = 0;
				$object->background_id = 0;
				$object->file_id = 0;
				$object->summary="";
			} else {
				$form->meta('id',$object->id);
				if ($object->publish == 0) $object->publish = null;
				if ($object->unpublish == 0) $object->unpublish = null;
			}

			//$form->register(null,'',new htmlcontrol('<br /><div class="moduletitle">'.$i18n['news_content'].'</div><hr size="1" />'));
			$form->register('title',$i18n['title'],new textcontrol($object->title));
			$form->register('summary',"Summary of news",new texteditorcontrol($object->summary, 5, 80));
			$form->register('body',$i18n['body'],new htmleditorcontrol($object->body));
			// Oct 22, 2010 - trung - remove background_id - begin
			$form->register('background_id','Large image on Feature tab', new ajax_select_file('background_id',0,$object->background_id,true));
			// Oct 22, 2010 - trung - remove background_id - end
			$form->register('file_id','News cover Image', new ajax_select_file("file_id",$config->image_group,$object->file_id,true));
			/**
			* Oct 28, 2010 - immanuel192 - add ajax_image_list control - begin
			*/
			//$form->register("images_list","Images List", new ajax_image_list("image_list",$config->image_group,$object->id,true));
			/**
			* Oct 28, 2010 - immanuel192 - add ajax_image_list control - end
			*/
			$form->register(null,'',new htmlcontrol('<br /><div class="moduletitle">'.$i18n['publish_information'].'</div><hr size="1" />'));
			//$form->register('publish',$i18n['publish'],new popupdatetimecontrol($object->publish,$i18n['nopublish']));
			$checked = empty($object->publish) ? true : false;
			$form->register('publish',$i18n['publish'],new yuidatetimecontrol($object->publish,$i18n['nopublish'], true, true, false, $checked));
			//$form->register('unpublish',$i18n['unpublish'],new popupdatetimecontrol($object->unpublish,$i18n['nounpublish']));
			$checked = empty($object->unpublish) ? true : false;

			$form->register('unpublish',$i18n['unpublish'],new yuidatetimecontrol($object->unpublish,$i18n['nounpublish'], true, true, false, $checked));
			// Feb 10, 2011 - trungdt - remove feature and tag - begin
			//$form->register('featured_header','',new htmlcontrol('<br /><div class="moduletitle">'.$i18n['featured_info'].'</div><hr size="1" />'));
			//$form->register('is_featured',$i18n['feature'],new checkboxcontrol($object->is_featured,true));

			//$form->register('tag_header','',new htmlcontrol('<br /><div class="moduletitle">'.$i18n['tags'].'</div><hr size="1" />'));
			// Feb 10, 2011 - trungdt - remove feature - end

			$form->register('submit','',new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));

			return $form;
		}

		function update($values,$object) {
			if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
			exponent_forms_initialize();

			$object->title = $values['title'];
			$object->summary = $values['summary'];
			// Feb 11, 2011 - trungdt - begin
			$internal_name=$tinter=_to_vietnamese_with_nomark($values['title']);
			if (!isset($object->id))
			{
				global $db;
				do {
					$temp=$db->selectObject("newsitem","internal_name = '{$tinter}'");
					if ($temp!=null)
					{// we can use this internal name
						$tinter=$internal_name.rand(rand(0,100),9999);
					}
					else
					{
						$internal_name=$tinter;
						break;   
					}
				}while (0<1);
				$object->internal_name=$internal_name;
			}
			// Feb 11, 2011 - trungdt - end
			// Oct 22, 2010 - remove background_id - trung - begin
			//$object->background_id = $values['background_id'];
			// Oct 22, 2010 - trung - remove background_id - end
			// Comment out this next one - there's no place in the form to fill it in.  No idea what the intention is.  Maia - 2/5/09
			//$object->internal_name = preg_replace('/--+/','-',preg_replace('/[^A-Za-z0-9_]/','-',$values['int']));
			$object->body = $values['body'];
			$object->file_id = intval($values['file_id']);
			$object->background_id = intval($values['background_id']);
			//$object->publish = popupdatetimecontrol::parseData('publish',$values);
			$object->publish = yuidatetimecontrol::parseData('publish',$values);
			//$object->unpublish = popupdatetimecontrol::parseData('unpublish',$values);
			$object->unpublish = yuidatetimecontrol::parseData('unpublish',$values);
			$object->is_featured = (isset($values['is_featured']) ? 1 : 0);

			return $object;
		}
	}

?>
